package com.kaigejava.juc.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author kaigejava
 * 重入锁demo
 */
public class ReentrantDemo {

    public static void main(String[] args) {
        message message = new message();
        for(int i=0;i<3;i++){
            new Thread(()->{
                message.sendMsg();
            },"线程"+(i+1)).start();
        }
    }
}

class message{

    ReentrantLock lock = new ReentrantLock(true);
    public  void sendMsg(){
        lock.lock();
        try {
            System.out.println("线程Id为："+Thread.currentThread().getId()+"的。开始调用发送短信方法");
            sendEmail();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public   void sendEmail(){
        lock.lock();

        try {
            System.out.println("===线程Id为："+Thread.currentThread().getId()+"的。开始调用发送邮件的方法");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}